import 'dart:async';
import 'package:path/path.dart';
import 'package:sqflite/sqflite.dart';

class SqlManager {
  static const  _VERSION = 1;
  static const  _NAME = "dianj.db";
  static Database? _database;
  final String nowTimeStr = DateTime.now().toString();

  SqlManager();

  //初始化
  static Future<void> init() async{
   var databasesPath = await getDatabasesPath();
   String dbName = _NAME;
   String path = join(databasesPath,dbName);
   print("init===> db path === $path");
   _database = await openDatabase(path,version: _VERSION,
      onCreate: (Database db,int version) async {
        //print("====> db onCreate  is ok");
      },
   );
   //print("===> init _database is null=== ${_database == null}",);
 }
 ///判断表是否存在
 static Future<bool> isTableExits(String tableName) async{
    await getCurrentDatabase();
    var res = await _database?.rawQuery(
      "select * from Sqlite_master where type = 'table' and name = '$tableName' "
    );
    return res != null && res.isNotEmpty ;
 }
 /// 获取当前db
  static Future<Database?> getCurrentDatabase() async{
   print("_database is null ======> ${_database == null} ");
   if(_database == null){
     await init();
   }
   return _database;
 }

 //关闭
 static void close(){
   _database?.close();
   _database = null;
 }



}